Uec3.PurchaseQueryWindowCreator = Ext.extend(Ext.app.Module,{
	id : 'purchaseQueryWindowCreator',
	type:'common/partnerQuery',
	
	createWindow : function(initParams){
		var desktop = this.app.getDesktop();
//		alert(initParams.partnerIdByBuyer);
		var main_win = desktop.getWindow("purchaseQueryWindow");
    	if( !main_win ){
    		main_win = desktop.createWindow({
				id : 'purchaseQueryWindow',
				modal : true,
				title : '引用采购订单',
				width : 700,
				height : 400,
				iconCls : 'acc-icon',
				animCollapse : false,
				constrainHeader : true,
				maximizable : false,
				minimizable : false,
				taskbuttonTooltip : "引用采购订单",
				layout : "fit",
//				tools : [{
//							id : 'refresh',
//							handler : Ext.emptyFn,
//							scope : this
//						}],
				items : []
			});
			
			main_win.partnerIdByBuyer = initParams.partnerCmb.getValue();
			
			var view_panel = this.createPanel(main_win);
			main_win.add(view_panel);
			
//			main_win.doLayout();
			main_win.show();
			
			
	    }
	    
	},
	selectRow : function(main_win, id) {
		var lookup = main_win.lookup;
		var record = main_win.show_pur_list_store.getById(id);
		if(lookup.lookupCallBackEvents) {
	    	Ext.each(lookup.lookupCallBackEvents, function(event) {
	    		if(event == 'lookupChange') {
	    			lookup.fireEvent(event, record);
	    		} else {
	    			lookup.fireEvent(event);
	    		}
	    	} );
	    };
	    
		main_win.creator.setLookupValue({
			'codeOfBuyer' : record.data.codeOfBuyer,
			'id' : record.data.id
		}, main_win);
		main_win.close();
	},
	
//	selectRow1 : function() {
//		var recs = this.show_pur_list_store.getRange();
//		Ext.each(recs, function(rec) {
//			var obj = Ext.get(PUR_OPTION_RECORD_ID + rec.id);
//			rec.main_win = this;
//			obj.on('click', function() {
//				var lookup = this.main_win.lookup;
//        		if(lookup.lookupCallBackEvents) {
//			    	Ext.each(lookup.lookupCallBackEvents, function(event) {
//			    		if(event == 'lookupChange') {
//			    			lookup.fireEvent(event, this);
//			    		} else {
//			    			lookup.fireEvent(event);
//			    		}
//			    	}.createDelegate(this) );
//			    };
//				
//				this.main_win.creator.setLookupValue({
//    				'codeOfBuyer' : this.data.codeOfBuyer,
//    				'id' : this.data.id
//				}, this.main_win);
//        		this.main_win.close();
//			}.createDelegate(rec) );
//			
//		}.createDelegate(this) );
//	
//	},
	createPurListStore : function() {
		var show_pur_list_record = Ext.data.Record.create( [
			{
				name : 'id'
			}, {
				name : 'codeOfBuyer'
			}, {
				name : 'partnerNameByBuyer'
			}, {
				name : 'status'
			}, {
				name : 'statusDes'
			}, {
				name : 'amount'
			}, {
				name : 'currentor'
			}, {
				name : 'creator'
			}, {
				name : 'gmtCreate'
			}, {
				name : 'submitter'
			}, {
				name : 'gmtSubmit'
			}, {
				name : 'deliveryer'
			}, {
				name : 'gmtDelivery'
			}, {
				name : 'receiverName'
			}, {
				name : 'gmtAccept'
			}, {
				name : 'memoOfBuyer'
			}
		] );
		
		var show_pur_list_store = new Ext.data.Store( {
			proxy : new Ext.data.HttpProxy(
				{
					url : Uec3.App.Constants.homeUrl + "/purchase/getAvailabeOrders.do",
					method : 'post'
				}
			),
			pruneModifiedRecords : true, 
			paramNames : {
				start : "start",
				limit : "limit"  
			},
			remoteSort:true,
			reader : new Ext.data.JsonReader(
				{
					totalProperty : 'totalProperty',
					root : 'root'
				}, 
				show_pur_list_record
			)
					
		} );
		
		return show_pur_list_store;
	},
	createPurListGridPanel : function() {
		
		function createOptionPurBtn(value, metadata, record, rowIndex, colIndex,
				store) {
			var windCreator = "Uec3.App.getModule('purchaseQueryWindowCreator')";
			var wind = "Uec3.App.getDesktop().getWindow('purchaseQueryWindow')";
			return '<a href="javascript:' + windCreator + '.selectRow(' + wind + ',' + record.data.id + ');">选中</a>';
		}
		
		var show_pur_list_colName = new Ext.grid.ColumnModel( [ 
			{
				header : '选择',
				id : 'exec',
				dataIndex : 'id',
				renderer : createOptionPurBtn.createDelegate(this)
			},	{
				header : '单 据 号',
				dataIndex : 'codeOfBuyer'
			}, {
				header : '卖      方',
				dataIndex : 'partnerNameByBuyer'
			}, {
				header : '创建人',
				dataIndex : 'creator'
			}, {
				header : '创建时间',
				dataIndex : 'gmtCreate'
			}, {
				header : '提交人',
				dataIndex : 'submitter'
			}, {
				header : '提交时间',
				dataIndex : 'gmtSubmit'
			}, 
//				{
//				header : '发货人',
//				dataIndex : 'deliveryer'
//			}, {
//				header : '发货时间',
//				dataIndex : 'gmtDelivery'
//			}, 
				{
				header : '备    注',
				dataIndex : 'memoOfBuyer'
			} 
		]);
		
//		var SHOW_PUR_LIST_GRID_ID = 'show_pur_query_grid_id';
		
		var show_pur_list_grid_panel = new Ext.grid.GridPanel( {
			title : '概要查询明细',
//			id : SHOW_PUR_LIST_GRID_ID,
			cm : show_pur_list_colName,
			store : this.show_pur_list_store,
			iconCls : 'icon-grid',
			loadMask : {
				msg : '数据加载中...'
			},
			viewConfig : {
				forceFit:true
			},
			height : 300,
			bbar: new Ext.PagingToolbar( {
	            pageSize : 10,
	        	store : this.show_pur_list_store,
	       	 	displayInfo : true,
	        	displayMsg : '第 {0} - {1} 条  共 {2} 条',
	        	emptyMsg : "没有记录"
	        	
	        } ),
	        listeners : {
	        	rowdblclick : function(g, i, e) {
	        		var lookup = this.lookup;
	        		if(lookup.lookupCallBackEvents) {
				    	Ext.each(lookup.lookupCallBackEvents, function(event) {
				    		if(event == 'lookupChange') {
				    			lookup.fireEvent(event, g.getStore().getAt(i));
				    		} else {
				    			lookup.fireEvent(event);
				    		}
				    	});
				    };
	        		
    				this.creator.setLookupValue({
	    				'codeOfBuyer' : g.getStore().getAt(i).data.codeOfBuyer,
	    				'id' : g.getStore().getAt(i).data.id
    				}, this);
	        		this.close();
	        	}.createDelegate(this)
	        }
		} );
		
//		Ext.util.Observable.capture(this.show_pur_list_store, function(e) {
//			if(e == 'load') {
//				this.creator.selectRow1.createDelegate(this)();
//			}
//		}.createDelegate(this));
		
		return show_pur_list_grid_panel;
		
	},
	createConditionPanel : function() {
		
		function getPurFormValues() {
			var form_values = this.advanced_search_form_panel.getForm().getValues();
	
			for (var j in form_values) {
				form_values[j] = castNoSelToNULL(form_values[j]);
			}

			form_values['partnerIdByBuyer'] = this.partnerIdByBuyer;
			
			form_values = Ext.util.JSON.encode(form_values);
	
			return form_values;
		}
		
		function searchResultByCondition() {
			var values = getPurFormValues.createDelegate(this)();
			
			if(values) {
	    		this.show_pur_list_store.baseParams = {conditions : values};
			} else {
				this.show_pur_list_store.baseParams = {};
			}
			this.show_pur_list_store.load();
		}
		
		var ds_pur_status_select = new Ext.data.JsonStore({
           url : Uec3.App.Constants.homeUrl + "/dictionary/findByVarGroup.do?group=order_status",
           root: "",
           autoLoad:true,
           fields:[{name:'varIndex',type:'int'},{name:'varName',type:'string'}]
        }); 
		
		
		var advanced_search_form_panel = new Ext.FormPanel({
			style : 'padding:0px; fontSize:13px; marginTop:5px',
			layout : 'hbox',
			frame : true,
			height : 40,			    
		         items:[
		            {
						xtype : 'label',
						text : '卖方:',
						style : {
							marginLeft : '1px',
							width : '50px'
						}
					}, {
						xtype : 'textfield',
						name : 'partnerNameByBuyer',
						style : "width : 90px"
					}, {
						xtype : 'label',
						text : '单据号:',
						style : {
							marginLeft : '5px',
							width : '55px'
						}
					}, {
						xtype : 'textfield',
						name : 'codeOfBuyer',
						style : "width : 90px"
					}, 
//						{
//						xtype:'label',
//					    text :'单据状态:',
//						style : {
//							marginLeft : '5px',
//							width : '70px'
//						}
//					}, {
//						xtype:'combo',
//				       allowBlank : false,
//					   name : 'status',
//				       valueField:'varIndex',
//                       displayField : 'varName',
//  				       mode : 'remote',
//  				       hiddenId : 'ds_pur_status_select_status',
//				       hiddenName:'status',
//				       store : ds_pur_status_select,
//				       selectOnFocus : true,
//				       triggerAction : 'all',
//				       loadingText : '加载中...',
//				       editable:false,
//				       emptyText : '请选择',
//					   width : 90
//					}, 
						{
						xtype : 'label',
						text : '开始时间:',
						style : {
							marginLeft : '5px',
							width : '55px'
						}
					}, {
						xtype:'datefield',
			               name:'gmtStart' ,
			               format:'Y-m-d',
						style : "width : 90px"
					}, {
						xtype : 'label',
						text : '结束时间:',
						style : {
							marginLeft : '5px',
							width : '55px'
						}
					}, {
						xtype:'datefield',
			               name:'gmtEnd' ,
			               format:'Y-m-d',
						style : "width : 90px"
					}, {
						xtype : 'button',
						text : '查    找',
						width : 40,
						style : {
							marginLeft : '10px'
						},
						listeners : {
							click : function() {
								searchResultByCondition.createDelegate(this)();
							}.createDelegate(this)
						}
					}
		         ]
		});
		return advanced_search_form_panel;
	},
	createPanel : function(main_win) {
		
		main_win.on('show', function() {
			main_win.show_pur_list_store.baseParams = {conditions :Ext.encode({partnerIdByBuyer:main_win.partnerIdByBuyer})};
			main_win.show_pur_list_store.load();
		}, main_win, {single : true});
		
		main_win.creator = this;
		main_win.show_pur_list_store = this.createPurListStore();
		main_win.advanced_search_form_panel = this.createConditionPanel.createDelegate(main_win)();
		main_win.show_pur_list_grid_panel = this.createPurListGridPanel.createDelegate(main_win)();
		
		main_win.view_panel = new Ext.Panel( {
		      items : [main_win.advanced_search_form_panel, main_win.show_pur_list_grid_panel]
		});
		
		return main_win.view_panel;
	}
});
